<template>
  <div class="aes-tool">
    <h2>AES 加解密</h2>
    <div class="input-group">
      <label for="inputText">输入文本:</label>
      <textarea
        id="inputText"
        v-model="inputText"
        placeholder="请输入待加密或待解密的文本"
        rows="5"
      ></textarea>
    </div>
    <div class="input-group">
      <label for="key">密钥:</label>
      <input
        id="key"
        type="text"
        v-model="key"
        placeholder="请输入密钥 (建议16位)"
      />
    </div>
    <div class="input-group">
      <label for="mode">加密模式:</label>
      <select id="mode" v-model="mode">
        <option value="CBC">CBC</option>
        <option value="ECB">ECB</option>
        <option value="CFB">CFB</option>
        <option value="OFB">OFB</option>
      </select>
    </div>
    <div class="input-group">
      <label for="padding">填充方式:</label>
      <select id="padding" v-model="padding">
        <option value="Pkcs7">Pkcs7</option>
        <option value="NoPadding">NoPadding</option>
        <option value="Iso97971">Iso97971</option>
      </select>
    </div>
    <div class="input-group">
      <label for="iv">初始向量 (IV):</label>
      <input id="iv" type="text" v-model="iv" placeholder="可选 (16个字符)" />
    </div>
    <div class="btn-group">
      <button @click="encryptText">AES 加密</button>
      <button @click="decryptText">AES 解密</button>
    </div>
    <div class="output-group" v-if="resultText">
      <h3>结果：</h3>
      <textarea v-model="resultText" rows="5" readonly></textarea>
    </div>
    <p v-if="errorMessage" class="error">{{ errorMessage }}</p>
  </div>
</template>

<script>
import CryptoJS from 'crypto-js';

export default {
  name: 'AESTool',
  data() {
    return {
      inputText: '',
      key: '',
      resultText: '',
      errorMessage: '',
      mode: 'CBC',
      padding: 'Pkcs7',
      iv: ''
    };
  },
  methods: {
    encryptText() {
      if (!this.inputText || !this.key) {
        this.errorMessage = '请输入文本和密钥';
        return;
      }
      try {
        this.errorMessage = '';
        const parsedKey = CryptoJS.enc.Utf8.parse(this.key);
        let options = {
          mode: CryptoJS.mode[this.mode],
          padding: CryptoJS.pad[this.padding] || CryptoJS.pad.Pkcs7
        };
        // 对于非 ECB 模式，如果没有提供 IV，则使用默认全 0 IV
        if (this.mode !== 'ECB') {
          if (this.iv) {
            options.iv = CryptoJS.enc.Utf8.parse(this.iv);
          } else {
            options.iv = CryptoJS.enc.Utf8.parse("0000000000000000");
          }
        }
        const encrypted = CryptoJS.AES.encrypt(this.inputText, parsedKey, options);
        this.resultText = encrypted.toString();
      } catch (error) {
        this.errorMessage = '加密失败：' + error.message;
      }
    },
    decryptText() {
      if (!this.inputText || !this.key) {
        this.errorMessage = '请输入加密文本和密钥';
        return;
      }
      try {
        this.errorMessage = '';
        const parsedKey = CryptoJS.enc.Utf8.parse(this.key);
        let options = {
          mode: CryptoJS.mode[this.mode],
          padding: CryptoJS.pad[this.padding] || CryptoJS.pad.Pkcs7
        };
        // 对于非 ECB 模式，如果没有提供 IV，则使用默认全 0 IV
        if (this.mode !== 'ECB') {
          if (this.iv) {
            options.iv = CryptoJS.enc.Utf8.parse(this.iv);
          } else {
            options.iv = CryptoJS.enc.Utf8.parse("0000000000000000");
          }
        }
        const decrypted = CryptoJS.AES.decrypt(this.inputText, parsedKey, options);
        const decryptedData = decrypted.toString(CryptoJS.enc.Utf8);
        if (!decryptedData) {
          this.errorMessage = '解密失败，请检查密钥、IV 和数据';
          this.resultText = '';
        } else {
          this.resultText = decryptedData;
        }
      } catch (error) {
        this.errorMessage = '解密失败：' + error.message;
      }
    }
  }
};
</script>

<style scoped>
.aes-tool {
  max-width: 600px;
  margin: 0 auto;
  text-align: center;
}
.input-group,
.output-group {
  margin: 10px 0;
}
label {
  display: block;
  margin: 5px 0;
  font-weight: bold;
}
textarea,
input[type="text"] {
  width: 100%;
  padding: 10px;
  margin: 5px 0;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  font-family: 'Arial', sans-serif;
  transition: border-color 0.3s;
}
textarea:focus,
input[type="text"]:focus {
  border-color: #3498db;
}
.btn-group {
  margin: 10px 0;
}
button {
  padding: 10px 20px;
  margin: 5px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}
button:hover {
  background-color: #2980b9;
}
.error {
  color: red;
  margin-top: 10px;
}
</style> 